package cn.itsource.org.controller;

import cn.itsource.basic.annotation.Repeat;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.config.exception.CustomException;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.User;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController //@Controller + @ResponseBody的结合体
@RequestMapping("/user") //暴露公共路径
@Api(tags = "用户管理接口")  //swagger API
public class UserController {
    @Autowired
    private IUserService service;

    /**
     *验证码接口
     * @return
     */
    @Repeat(2)
    @GetMapping("/sendmobilecode/{phone}")
    @ApiOperation(value = "验证码接口") //swagger API
    public AjaxResult sendmobilecode(@PathVariable("phone")String phone, HttpServletRequest request){
         service.sendmobilecode(phone,request);
        return AjaxResult.createSuccess("验证码发送成功!");

    }
    /**
     * 手机号校验接口
     */

    @GetMapping("/checkphone/{phone}")
    @ApiOperation(value = "手机号校验") //swagger API
    public AjaxResult checkphone(@PathVariable("phone")String phone){
        try {
            AjaxResult checkphone = service.checkphone(phone);
            return AjaxResult.createSuccess(checkphone);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.createError(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }
    /**
     *注册接口
     */
    @PostMapping
    @ApiOperation(value = "注册接口") //swagger API
    public AjaxResult register(@RequestBody @Validated(User.PhoneRegister.class) User user){
        try {
            service.register(user);
            return AjaxResult.createSuccess();
        } catch (CustomException e) {
            e.printStackTrace();
            return AjaxResult.createError(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

}
